﻿using System;
using System.Collections.Generic;

using CtpTrader.Logic.Core.Common;
using CtpTrader.Logic.Core.Trade;

namespace CtpTrader.Logic.Core.Strategy
{
    public class Test1 : StrategyBase
    {

        public override string PolicyChineseName
        {
            get { return "测试策略1"; }
        }

        /// <summary>
        /// Bar新开事件
        /// </summary>
        public override void BarOpen(Quote.BarSeries series)
        {
            if (series[1] != null && series[2] != null && series[3] != null && series[4] != null)
            {
                PositionUnit unit   = new PositionUnit();
                unit.Strategy       = this.PolicyName;
                unit.Account        = AccountManager.GetSimulationAccount();
                unit.Instrument     = series.Instrument;
                unit.BarUnit        = series.BarUnit;
                unit.DirectionType  = DirectionType.Buy;

                Position position = PositionManager.GetInstance().HasPosition(unit);

                //开仓
                if (position == null )
                {
                    if(series[1].Low > series[2].Low && series[2].Low > series[3].Low && series[3].Low > series[4].Low)
                    {
                        position = new Position(unit);
                        position.Open(series[0].Open, 1);

                        PositionManager.GetInstance().Add(position);
                    }
                }
                //加仓
                else
                {
                    if (series[1].Low > series[2].Low)
                    {
                        position.Open(series[0].Open, 1);
                    }
                }
            }
        }

        /// <summary>
        /// Bar完成事件
        /// </summary>
        public override void BarClose(Quote.BarSeries series)
        {
            
        }

        /// <summary>
        /// Bar更新事件
        /// </summary>
        public override void BarUpdate(Quote.BarSeries series)
        {
            PositionUnit unit = this.GetUnit(AccountManager.GetSimulationAccount(), series, DirectionType.Buy);

            Position position = PositionManager.GetInstance().HasPosition(unit);

            if (position != null && position.Volume > 0)
            {
                if (series[1] != null && series[0].Close < series[1].Low)
                {
                    position.Close(series[0].Close, position.Volume);
                }
            }
        }
    }
}
